<!--
 * @Author: your name
 * @Date: 2021-08-06 15:38:44
 * @LastEditTime: 2021-08-18 16:04:26
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \系统学习\VueProject\vue02\tabbar\src\views\home\Home.vue
-->
<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
        :title="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl1"
        class="tabC"
        v-show="isTabFixed"
    />
    <scroll class="content" 
            ref="scroll"
            :probe-type="3" 
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp = "loadMore">
      <home-swiper :banners="banners"
                   @swiperImageLoad="swiperImageLoad" />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :title="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <!-- 下面传进去的类型不能写死，应该根据点击了哪个类型就传进哪个类型的数据 -->
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- 当我们需要监听一个组件的原生事件时，必须给对应的事件加上.native修饰符才能进行监听 -->
    <back-top @click.native="backClick" v-show="isShowBackTop" />

    
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import Scroll from "components/common/scroll/Scroll";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import BackTop from "components/content/backTop/BackTop"

import HomeSwiper from "./childComps/HomeSwiper.vue";
import RecommendView from "./childComps/RecommendView.vue";
import FeatureView from "./childComps/FeatureView.vue";

import { getHomeMultidata, getHomeGoods } from "network/home";
import {debounce} from 'common/utils'
import {itemListenerMixin} from 'common/mixin'


export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop", // 默认传进去的就是pop
      isShowBackTop:false, // 是否显示返回按钮
      tabOffsetTop:0, 
      isTabFixed:false, // 是否吸顶
      saveY:0, // 记录离开时的位置
    };
  },
  mixins:[itemListenerMixin],
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  components: {
    NavBar,
    Scroll,
    TabControl,
    GoodsList,
    BackTop,
    HomeSwiper,
    RecommendView,
    FeatureView,
  },
  destroyed(){
    console.log('home destroyed');
  },
  // 回到首页时把刚才保存的位置设置回去
  activated(){
    // console.log('activated');
    this.$refs.scroll.scrollTo(0,this.saveY,0)
    this.$refs.scroll.refresh()
  },
  // 非活跃状态(离开时)
  deactivated(){
    // console.log('deactivated');
    // 1. 记录位置
    this.saveY = this.$refs.scroll.getScrollY()
    // 3. 取消全局事件的监听
    this.$bus.$off('itemImageLoad',this.itemImgListener)
  },
  // 组件创建完之后就发送网络请求
  created() {
    // 1. 请求多个数据
    this.getHomeMultidata();
    // 2. 请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
    // 
  },
  mounted(){
  
  },
  methods: {
    /*事件监听相关的方法 */
   
    // 监听TabControl传出来的点击事件
    tabClick(index) {
      // console.log(index);
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      // 和最新点击的index保持一致
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    backClick(){
      // 通过这种方法可以访问到Scroll组件中的属性和方法
      // console.log(this.$refs.scroll.message);
      this.$refs.scroll.scrollTo(0,0)
    },
    contentScroll(position){
      // console.log(position);
      // 1. 判断backTop是否显示
      this.isShowBackTop = (-position.y) > 1000
      // 2. 决定tabControl是否吸顶(position:fixed)
      this.isTabFixed = (-position.y) > this.tabOffsetTop
    },
    loadMore(){
      this.getHomeGoods(this.currentType)
    },
    // 获取tabControl的offsetTop
    // 所有的组件都有一个属性$el，用于获取组件中的元素
    swiperImageLoad(){
      // console.log(this.$refs.tabControl2.$el.offsetTop);
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
    

    /*
    网络请求相关的方法
    */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        // 这里的数据属于局部变量，函数执行完就会被销毁，所以在执行完成前要保存起来
        //   console.log(res);
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        
        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp()
      });
    },
  },
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9; */
}
/* 在浏览器原生滚动时，为了让导航不跟随一起滚动 */
/* 但我们这个项目使用的是better-scroll的局部滚动，下面这些属性就没有用了 */
/* .content{
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
} */
.tabC{
  position: relative;
  z-index: 999;
}
.content{
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}

</style>
